package com.foreverwin.jzymes.pms.listener;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.foreverwin.jzymes.common.mapper.ResourceAdviceMapper;
import com.foreverwin.jzymes.common.service.IActivityRuleAdviceService;
import com.foreverwin.jzymes.common.service.IResourceAdviceService;
import com.foreverwin.jzymes.framework.annotation.MessageLog;
import com.foreverwin.jzymes.framework.event.AppEvent;
import com.foreverwin.jzymes.framework.util.DateUtil;
import org.apache.activemq.command.ActiveMQTopic;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
public class PmsListener {

    @Autowired
    IResourceAdviceService resourceAdviceService;
    @Autowired
    IActivityRuleAdviceService activityRuleAdviceService;
    @Autowired
    ResourceAdviceMapper resourceAdviceMapper;
    @Autowired
    JmsMessagingTemplate jmsTemplate;

    @JmsListener( destination = "PMS_SYNCHRO_INFRA_FROM_MES" )
    @MessageLog
    public String pmsSyncResourceInfo( String msg ){
        JSONObject map = JSONObject.parseObject( msg );
        String site = map.getString( "site" );
        Date startDate = map.getDate( "startTime" );
        Date endDate = map.getDate( "endTime" );
        String syncType = map.getString( "synchroType" );

        JSONArray jsonArray = new JSONArray();
        JSONObject rtnJson = new JSONObject();
        if( "equip".equals( syncType ) ){
            List<Map<String,Object>> resourceList = resourceAdviceMapper.findResourceListByDate( site, startDate, endDate );
            for( Map<String,Object> resourceMap : resourceList ){
                JSONObject resourceJson = new JSONObject();
                resourceJson.put( "id", resourceMap.get( "RESRCE" ) );
                resourceJson.put( "equipName", resourceMap.get( "DESCRIPTION" ) );
                resourceJson.put( "equipGroupId", "" );
                String status = resourceAdviceService.findResourceStatus( site, (String) resourceMap.get( "RESRCE" ));
                if (StringUtils.isEmpty( status ) ){
                    resourceJson.put( "mesStatus", "" );
                }else {
                    resourceJson.put( "mesStatus", status );
                }
                //resourceJson.put( "mesStatus", resourceAdviceService.findResourceStatus( site, (String) resourceMap.get( "RESRCE" )) );
                jsonArray.add( resourceJson );
            }
        }
        rtnJson.put("startTime",startDate);
        rtnJson.put("endTime",endDate);
        rtnJson.put("synchroType","equip");
        rtnJson.put("equips",jsonArray);

        return rtnJson.toJSONString();  //jsonArray.toJSONString();
    }

    @JmsListener( destination = "PMS_SYNCHRO_EQUIP_STATUS" )
    @MessageLog
    public String pmsSyncResourceStatus( String msg ){
        JSONObject map = JSONObject.parseObject( msg );
        String site = map.getString( "site" );
        String resrce = map.getString( "equipId" );
        String status = map.getString( "status" );

        try{
            if( "MA_START".equalsIgnoreCase( status ) ){
                status = "TPM";
            }else if( "MA_COMP".equalsIgnoreCase( status ) ){
                status = "IDLE";
            }else if( "REPAIR_START".equalsIgnoreCase( status ) ){
                status = "REPAIR";
            }else if( "REPAIR_COMP".equalsIgnoreCase( status ) ){
                status = "IDLE";
            }

            /**cnc设备要遵循设备状态转换规则，其他设备状态以上传状态为准*/
            if( resourceAdviceService.isCNC( site, resrce ) ){
                resourceAdviceService.changeResourceStatus( site, resrce, status );
            }else{
                resourceAdviceService.updateResourceStatus( site, resrce, status );
            }

            /**发送设备状态变化到页面*/
            JSONObject eventData = new JSONObject();
            eventData.put( "SITE", site );
            eventData.put( "RESRCE", resrce );
            eventData.put( "STATUS", status );
            AppEvent<JSONObject> appEvent = new AppEvent<>( this );
            appEvent.setName( "RESOURCE_STATUS_CHANGE" );
            appEvent.setData( eventData );
            ActiveMQTopic topic = new ActiveMQTopic( "MES_APP_EVENT_INFRA" );
            jmsTemplate.convertAndSend( topic, appEvent );

            JSONObject success = new JSONObject();
            success.put( "equipId", resrce );
            success.put( "success", true );
            success.put( "msg", "" );
            success.put( "sendTime", DateUtil.format( new Date(), "yyyy-MM-dd" ) );
            return success.toString();
        }catch ( Exception e ){
            JSONObject success = new JSONObject();
            success.put( "equipId", resrce );
            success.put( "success", false );
            success.put( "msg", e.getMessage() );
            success.put( "sendTime", DateUtil.format( new Date(), "yyyy-MM-dd" ) );
            return success.toString();
        }

    }

    @JmsListener( destination = "PMS_SYNCHRO_PLAN_EQUIP" )
    public String pmsSyncPlanEquipQty(){
        String planEquip = activityRuleAdviceService.findActivityRule( "Z_DB_ALL_EQU_OEE", "PLAN_EQUIP" );
        return planEquip;
    }
}
